package com.aifeng.hassan_food.sale

import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.base.addItems
import com.aifeng.base.loadLinear
import com.aifeng.base.refresh
import com.aifeng.common_ui.base.getColorEx
import com.aifeng.common_ui.base.gone
import com.aifeng.common_ui.base.visible
import com.aifeng.common_ui.helper.dp2px
import com.aifeng.common_ui.helper.getString
import com.aifeng.common_ui.helper.sp2px
import com.aifeng.hassan_food.R
import com.aifeng.model.CommonData
import com.aifeng.utils.NormalDecoration
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeByFinally
import com.aifeng.viewmodel.OrderViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HistoryActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "历史数据", divider = false)

        swipe_refresh.isRefreshing = true
        getData()
    }

    override fun getViewModel() = getViewModel<OrderViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        empty_hint.text = "暂无相关历史信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh)

            addItemDecoration(object : NormalDecoration() {
                override fun getHeaderName(pos: Int): String =
                    (mList[pos] as CommonData).title
            }.apply {
                setHeaderContentColor(getColorEx(R.color.bg_gray))
                setHeaderHeight(dp2px(44f))
                setTextSize(sp2px(14f))
                setTextPaddingLeft(dp2px(20f))
                setTextColor(getColorEx(R.color.black))
            })
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_history_list) { data, injector ->

                val index = mList.indexOf(data)
                val isLast = index == mList.size - 1

                injector.text(R.id.item_history_title, data.content)
                    .visibility(
                        R.id.item_history_divider,
                        if (!isLast && data.title != (mList[index + 1] as CommonData).title) View.GONE else View.VISIBLE
                    )
                    .clicked(R.id.item_history) {
                        startActivity<HistoryDetailActivity>(
                            "date" to data.create_date,
                            "hint" to (data.title + data.content)
                        )
                    }
            }
            .attachTo(recycle_list)
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().historyList(getString("createData"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                onSuccess = {
                    mList.clear()
                    mList.addItems(it)
                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

}
